# This Python file uses the following encoding: utf-8
import threading
from MySignals import global_ms
from db import ObjLink
from catchObj.Stocks import Stocks
import func

class TR(threading.Thread):
    threads = []
    hurl = 'https://hq.sinajs.cn/list='
    
    #类函数,用来初始化公共部分
    @classmethod
    def init(cls,threadsNum):
        i=0
        while True:
            if i>=threadsNum:
                break
            cls(i)
            i+=1


    def __init__(self,cls):
        super(TR, self).__init__()
        self.flag = threading.Event()     # 用于暂停线程的标识
        self.running = threading.Event()      # 用于停止线程的标识
        self.running.set()      # 将running设置为True
        cls.statusName = ""    #状态名称
        cls.threads.append(self)

    @classmethod
    def Tstart(cls):
        for i in cls.threads:
            i.start()     # 设置为False, 让线程阻塞
        cls.statusName = "运行"


    @classmethod
    def pause(cls):
        for i in cls.threads:
            i.flag.clear()     # 设置为False, 让线程阻塞
        cls.statusName = "暂停"

    @classmethod
    def resume(cls):
        for i in cls.threads:
            i.flag.set()    # 设置为True, 让线程停止阻塞
        cls.statusName = "运行"

    @classmethod
    def stop(cls):
        for i in cls.threads:
            i.flag.set()       # 将线程从暂停状态恢复, 如何已经暂停的话
            i.running.clear()        # 设置为False
        cls.statusName = "停止"
        cls.threads = []
        

    #通知接口
    def notice_msg(self,msg):
        global_ms.setCatchMsg.emit("线程"+str(self.threadId)+":"+msg)

    #整理数据,清空实时数据,presStr为""或者"fs_"
    def clearMongoData(presStr):
        for st in Stocks.stocks:
            Stocks.redis.delete(presStr+func.zhuanhuan(st[0]))

    #获取url
    @classmethod
    def getUrls(cls, stocks):
        s=""
        for st in stocks:
            s = s + func.zhuanhuan(st[0]) + ","
        return  cls.hurl + s
